package dr.common.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
@Getter
@Setter
@Component
public class AliyunSmsUtil {

    @Value("${aliyun.sms.regionId}")
    String regionId = "";
    @Value("${aliyun.sms.accessKeyId}")
    String accessKeyId = "";
    @Value("${aliyun.sms.accessSecret}")
    String accessSecret = "";
    @Value("${aliyun.sms.domain}")
    String domain = "";
    @Value("${aliyun.sms.version}")
    String version = "";
    @Value("${aliyun.sms.action}")
    String action = "";
    @Value("${aliyun.sms.SignName}")
    String SignName = "";

    public String sendSms(String mobile, String TemplateCode, String param) {

        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(domain);
        request.setVersion(version);
        request.setAction(action);
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", SignName);
        request.putQueryParameter("TemplateCode", TemplateCode);
        request.putQueryParameter("TemplateParam", param);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ClientException e) {
            log.error("mobile:{},TemplateCode:{},param:{}",mobile, TemplateCode, param, e.getLocalizedMessage());
        } finally {
            client.shutdown();
        }

        return "";
    }
}
